package org.luxor.commons.core.utils;

import org.springframework.security.core.context.SecurityContextHolder;

/**
 * SecurityContext 工具类
 *
 * @author XinmingYan @date 2020/04/28 23:09
 */
public class SecurityContextUtils {

    public static String getUsername() {
        try {
            Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            return Jackson2Utils.toJsonObject(principal).get("username").asText("anonymous");
        } catch (Exception e) {
            return "anonymous";
        }
    }

    public static Long getTenantId() {
        try {
            Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            return Jackson2Utils.toJsonObject(principal).get("tenantId").asLong(1L);
        } catch (Exception e) {
            return 1L;
        }
    }
}
